package main

import (
	"github.com/gin-gonic/gin"

	"github.com/DeanThompson/ginpprof"

	"com.jiaguyu/controller/account"
	"com.jiaguyu/controller/category"
	"com.jiaguyu/controller/comment"

	"com.jiaguyu/controller/ask"

	"com.jiaguyu/controller/answer"
	"com.jiaguyu/controller/favorite"

	"com.jiaguyu/dao/db"
	"com.jiaguyu/id_gen"

	"com.jiaguyu/logger"

	"com.jiaguyu/filter"
	maccount "com.jiaguyu/middleware/account"
)

func InitAccount() (err error) {
	maccount.InitSession("memory", "")
	return
}

func InitDB() (err error) {
	dns := "root:123456@tcp(localhost:3306)/jiaguyu?parseTime=true"
	err = db.Init(dns)
	if err != nil {
		return
	}
	return
}

func InitTemplate(router *gin.Engine) {
	router.StaticFile("/", "./static/index.html")
	router.StaticFile("/favicon.ico", "./static/favicon.ico")
	router.Static("/css/", "./static/css/")
	router.Static("/fonts/", "./static/fonts/")
	router.Static("/img/", "./static/img/")
	router.Static("/js/", "./static/js/")
}

func InitFilter() (err error) {
	err = filter.Init("./data/filter.dat.txt")
	if err != nil {
		return
	}
	return
}

func InitLogger() {
	config := make(map[string]string)
	config["log_level"] = "debug"
	logger.InitLogger("console", config)
}

func main() {
	router := gin.Default()
	//日志初始化
	InitLogger()
	//数据库初始化
	err := InitDB()
	if err != nil {
		panic(err)
	}
	err = InitAccount()
	if err != nil {
		panic(err)
	}
	err = id_gen.Init(1)
	if err != nil {
		panic(err)
	}
	err = InitFilter()
	if err != nil {
		panic(err)
	}
	ginpprof.Wrapper(router)
	InitTemplate(router)
	//登录
	router.POST("/api/user/login", account.LoginHandle)
	//注册
	router.POST("/api/user/register", account.RegisterHandle)
	//分类查询
	router.GET("/api/category/list", category.GetCategoryListHandle)
	//问题的提问发布
	router.POST("/api/ask/submit", maccount.AuthMiddleware, ask.QuestionSubmitHandle)

	router.GET("/api/question/list", category.GetQuestionListHandle)

	//xaingqing
	router.GET("/api/question/detail", ask.GetQuestionListDetailHandle)
	router.GET("/api/answer/list", answer.AnswerQuestionListHandle)

	//评论
	//commentGroup :=router.Group("/api/comment/")
	commentGroup := router.Group("/api/comment/", maccount.AuthMiddleware)

	commentGroup.POST("/post_comment", comment.PostCommentHandle)
	commentGroup.POST("/post_reply", comment.PostCommentReplyHandle)
	//第一级的评论
	commentGroup.GET("/list", comment.CommentListHandle)
	//第二级回复评论
	commentGroup.GET("/reply_list", comment.ReplyListHandle)
	//点赞
	commentGroup.POST("/like", comment.CommentLikeHandle)

	//收藏 模块
	favoriteGroup := router.Group("/api/favorite/", maccount.AuthMiddleware)
	//favoriteGroup := router.Group("/api/favorite/")
	//创建收藏分类文件
	favoriteGroup.POST("/add_dir", favorite.AddDirHandle)
	//添加收藏
	favoriteGroup.POST("/add", favorite.AddHandle)
	//收藏文件的获取
	favoriteGroup.GET("/dir_list", favorite.DirListHandle)
	//收藏的列表
	favoriteGroup.GET("/list", favorite.ListHandle)
	//监听端口
	router.Run(":9090")
}
